package com.threaddemo4;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;

public class EitherDemo {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        CompletableFuture<String> f1 = CompletableFuture.supplyAsync(()->{
            System.out.println("F2 " + Thread.currentThread().getName());
            //int i = 10 / 0;
            return "F1 返回值";
        });

        CompletableFuture<String> f2 = CompletableFuture.supplyAsync(()->{
            System.out.println("F1 " + Thread.currentThread().getName());
            //int i = 10 / 0;
            return "F2 返回值";
        });

        //applyToEither:其中一个执行成功 就能执行
        //接收到的参数:是成功的那个参数 如果两个都执行成功 那么是前面一个成功的任务返回值作为参数
        //有返回值
        CompletableFuture<String> ate = f1.applyToEither(f2,r->{
            System.out.println("r = " + r);
           return "ate 返回值";
        });

        //acceptEither:其中一个执行成功 就能执行
        //接收到的参数:是成功的那个参数 如果两个都执行成功 那么是前面一个成功的任务返回值作为参数
        //没有返回值
        CompletableFuture ae = f1.acceptEither(f2,r->{
            System.out.println("ae = " + r);
        });

        CompletableFuture rae = f1.runAfterEither(f2,()->{
            System.out.println("rae 执行 ");
        });

        System.out.println("f1 = " + f1.get());
        System.out.println("f2 = " + f2.get());
        System.out.println("ate = " + ate.get());
        System.out.println("ae = " + ae.get());
        System.out.println("rae = " + rae.get());

    }
}
